package fileReader.demo.reader;

import java.awt.Graphics;
import java.awt.geom.Point2D;



/**
 * Edge is a line between two verteces, it can be used for counting the cost to use dijkstra
 */

class Edge
{	
	private Vertex start;
    private Vertex end;
    private double cost;
    private boolean HasColinealPoints;
	
	/**
     * Constructor for the class CEdge.
     * @param x1 x-value of point 1
     * @param y1 y-value of point 1
     * @param x2 x-value of point 2
     * @param y2 y-value of point 2
     */
	public Edge(Point2D p1, Point2D p2)
	{
		start = new Vertex(p1, null);
		end = new Vertex(p2, null);
	}
	
	/**
	 * Constructor for the class CEdge.
	 * @param cvertex vertex.
	 * @param cvertex1 vertex.
	 */
    public Edge(Vertex cvertex, Vertex cvertex1)
    {
        if(cvertex.getX() < cvertex1.getX())
        {
            start = cvertex;
            end = cvertex1;
            return;
        } else
        {
            start = cvertex1;
            end = cvertex;
            return;
        }
    }
	
	/**
	 * @return Returns the cost.
	 */
	public double getCost() {
		return cost;
	}
	/**
	 * @param cost The cost to set.
	 */
	public void setCost(double cost) {
		this.cost = cost;
	}
	/**
	 * @return Returns the end.
	 */
	public Vertex getEnd() {
		return end;
	}
	/**
	 * @param end The end to set.
	 */
	public void setEnd(Vertex end) {
		this.end = end;
	}
	/**
	 * @return Returns the hasColinealPoints.
	 */
	public boolean isHasColinealPoints() {
		return HasColinealPoints;
	}
	/**
	 * @param hasColinealPoints The hasColinealPoints to set.
	 */
	public void setHasColinealPoints(boolean hasColinealPoints) {
		HasColinealPoints = hasColinealPoints;
	}
	/**
	 * @return Returns the start.
	 */
	public Vertex getStart() {
		return start;
	}
	/**
	 * @param start The start to set.
	 */
	public void setStart(Vertex start) {
		this.start = start;
	}
    
    

    /**
     * Checks that are two edges accrossing each other. 
     * @param cedge edge
     * @return boolean intersects or not
     */
    public boolean Intersects(Edge cedge)
    {
           	
        double d = start.getX();
        double d1 = start.getY();
        double d2 = end.getX();
        double d3 = end.getY();
        
        if(start.getY() < end.getY())
        {
            d = end.getX();
            d1 = end.getY();
            d2 = start.getX();
            d3 = start.getY();
        }
        
        double d4 = cedge.start.getX();
        double d5 = cedge.start.getY();
        double d6 = cedge.end.getX();
        double d7 = cedge.end.getY();
       
        if(cedge.start.getY() < cedge.end.getY())
        {
            d4 = cedge.end.getX();
            d5 = cedge.end.getY();
            d6 = cedge.start.getX();
            d7 = cedge.start.getY();
        }
        
      	              	
        if(d3 > d5 || d1 < d7)
            return false;
        if(start.getX() > cedge.start.getX() && start.getX() > cedge.end.getX() && end.getX() > cedge.start.getX() && end.getX() > cedge.end.getX())
            return false;
        if(start.getX() < cedge.start.getX() && start.getX() < cedge.end.getX() && end.getX() < cedge.start.getX() && end.getX() < cedge.end.getX())
            return false;
                
        double d8;
        if(d1 < d5)
            d8 = cedge.GetX(d1) - d;
        else
            d8 = d4 - GetX(d5);
        double d9;
        if(d3 > d7)
            d9 = cedge.GetX(d3) - d2;
        else
            d9 = d6 - GetX(d7);
        

        
        if (d8>0.0D && d9 < 0.0D){

        	return true;
        }
       
        if(d8 < 0.0D && d9 > 0.0D){

        	return true;
        }

        return false;	
    }

    /**
     * Draws the edge to the sctreen.
     * @param g graphics
     * @param scale_factor scale
     * @param xtranslation translation
     * @param ytranslation translation
     * @param zoom zoom value
     */
    public void Draw(Graphics g,double scale_factor,int xtranslation,int ytranslation,double zoom)
    {
    	int x1,y1,x2,y2;
    	x1=(int)(((start.getX()*scale_factor)*zoom)+xtranslation);
    	y1=(int)(((start.getY()*scale_factor)*zoom)+ytranslation);
    	x2=(int)(((end.getX()*scale_factor)*zoom)+xtranslation);
    	y2=(int)(((end.getY()*scale_factor)*zoom)+ytranslation);
    	g.drawLine(x1,y1,x2,y2);
    }

    /**
     * Returns x. ??
     * @param d 
     * @return ?? mithn tmkin nyt tekee oikein?
     */
   public double GetX(double d)
    {
        double d1 = end.getX() - start.getX();
        double d2 = end.getY() - start.getY();
        double d3 = start.getX() + (d1 * (d - start.getY())) / d2;
        return d3;
    }

   /**
    * no tmmsi laskentoja ??
    * @return cost
    */
   public double Longitude()
    {
	  //Calculing cost of the edge related to its distance.
        cost = Math.sqrt((end.getX() - start.getX()) * (end.getX() - start.getX()) + (end.getY() - start.getY()) * (end.getY() - start.getY()));
        return cost;
    }
   
   /**
    * Returns the opposite of the vertex
    * @param cvertex get opposite from this vertex
    * @return value start or end
    */
    public Vertex getOpposite(Vertex cvertex)
    {
        if(cvertex == start)
            return end;
        else
            return start;
    }
    
    /**
     * Equivalent ???
     * @param cedge
     * @return boolean equivalent 
     */
    public boolean Equivalent(Edge cedge)
    {
        if(cedge == this)
            return true;
        return cedge.start == start && cedge.end == end || cedge.start == end && cedge.end == start;
    }
}